package com.jeesite.modules.wx.web;

import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
public class IndexController {


    @RequestMapping("/")
    void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/a");
    }

    @RequestMapping("/user")
    public void user(HttpServletResponse response) throws IOException {
        User user = UserUtils.getUser();
        Employee employee = (Employee) user.getRefObj();
        System.out.println(employee.getId());
        System.out.println(employee.getOffice().getOfficeCode());
        System.out.println(employee.getOffice().getOfficeName());
        System.out.println(employee.getOffice());

    }



    /**
     *
     * @Title: 构造图片
     * @Description: 生成水印并返回java.awt.image.BufferedImage
     * @param file
     *            源文件(图片)
     * @param waterFile
     *            水印文件(图片)
     * @param x
     *            距离右下角的X偏移量
     * @param y
     *            距离右下角的Y偏移量
     * @param alpha
     *            透明度, 选择值从0.0~1.0: 完全透明~完全不透明
     * @return BufferedImage
     * @throws IOException
     */
    public static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException {
        // 获取底图
        BufferedImage buffImg = ImageIO.read(file);
        // 获取层图
        BufferedImage waterImg = ImageIO.read(waterFile);
        // 创建Graphics2D对象，用在底图对象上绘图
        Graphics2D g2d = buffImg.createGraphics();
        int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
        int waterImgHeight = waterImg.getHeight();// 获取层图的高度
        // 在图形和图像中实现混合和透明效果
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        // 绘制
//        x = waterImgHeight;
//        y = waterImgWidth;

        int bufimgWidth = buffImg.getWidth();
        int bufimgHeight = buffImg.getHeight();

        System.out.println(bufimgWidth);
        System.out.println(bufimgHeight);

        g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
        g2d.dispose();// 释放图形上下文使用的系统资源
        return buffImg;
    }

    /**
     * 输出水印图片
     *
     * @param buffImg
     *            图像加水印之后的BufferedImage对象
     * @param savePath
     *            图像加水印之后的保存路径
     */
    private static void generateWaterFile(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".") + 1;
        try {
            ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     *
     * @param args
     * @throws IOException
     *             IO异常直接抛出了
     * @author bls
     */
    public static void main(String[] args) throws Exception {
        String sourceFilePath = "/Users/qing/Desktop/b.jpg";
        String waterFilePath = "/Users/qing/Desktop/aa.jpg";
        String headFilePath = "/Users/qing/Desktop/c.jpg";
        zoomImage(headFilePath,headFilePath,50,50);
        String saveFilePath = "/Users/qing/Desktop/test.jpg";
        // 增加二维码
        BufferedImage buffImg = watermark(new File(sourceFilePath), new File(waterFilePath), 80, 600, 1.0f);
        // 输出水印图片
        generateWaterFile(buffImg, saveFilePath);

        //头像加上
        BufferedImage headImgbuffImg = watermark(new File(saveFilePath), new File(headFilePath), 200, 600, 1.0f);
        generateWaterFile(headImgbuffImg, saveFilePath);


        createStringMark("/Users/qing/Desktop/test.jpg", "AAAwjx","/Users/qing/Desktop/test.jpg");


    }


    /*
     * 图片缩放,w，h为缩放的目标宽度和高度
     * src为源文件目录，dest为缩放后保存目录
     */
    public static void zoomImage(String src,String dest,int w,int h) throws Exception {

        double wr=0,hr=0;
        File srcFile = new File(src);
        File destFile = new File(dest);

        BufferedImage bufImg = ImageIO.read(srcFile); //读取图片
        Image Itemp = bufImg.getScaledInstance(w, h, bufImg.SCALE_SMOOTH);//设置缩放目标图片模板

        wr=w*1.0/bufImg.getWidth();     //获取缩放比例
        hr=h*1.0 / bufImg.getHeight();

        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
        Itemp = ato.filter(bufImg, null);
        try {
            ImageIO.write((BufferedImage) Itemp,dest.substring(dest.lastIndexOf(".")+1), destFile); //写入缩减后的图片
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @param filePath 源图片路径
     * @param markContent 图片中添加内容
     * @param outPath  输出图片路径
     * 字体颜色等在函数内部实现的
     */
    //给jpg添加文字
    public static boolean createStringMark(String filePath,String markContent,String outPath)
    {
        ImageIcon imgIcon=new ImageIcon(filePath);
        Image theImg =imgIcon.getImage();
        int width=theImg.getWidth(null)==-1?200:theImg.getWidth(null);
        int height= theImg.getHeight(null)==-1?200:theImg.getHeight(null);
        System.out.println(width);
        System.out.println(height);
        System.out.println(theImg);
        BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g=bimage.createGraphics();

        Color mycolor = Color.white;
        g.setColor(mycolor);
        g.setBackground(Color.white);
        g.drawImage(theImg, 0, 0, null );
        g.setFont(new Font("宋体",Font.PLAIN,15)); //字体、字型、字号
        g.drawString(markContent,200,670); //画文字
        g.dispose();
        try
        {
            FileOutputStream out=new FileOutputStream(outPath); //先用一个特定的输出文件名
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
//            param.setQuality(100, true);  //
//            encoder.encode(bimage, param);
            out.close();
        }
        catch(Exception e)
        {
            return false;
        }

        return true;
    }


}

